package com.star.uc.contract.enums.monitor;

import com.star.common.core.context.SpringContext;
import com.star.common.core.contract.enums.IEnum;
import com.star.common.core.contract.enums.ResultCodeEnum;
import com.star.common.core.contract.exception.ServiceException;
import com.star.common.kit.enums.EnumValueResolver;
import com.star.uc.contract.dto.request.MonitorRequest;
import com.star.uc.infras.properties.MonitorProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * 监控枚举
 * <p>
 * 如果需要根据code获取desc，请使用
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/11/17 18:22
 * @see com.star.common.kit.enums.EnumUtils#getByCode(java.lang.Class, java.lang.Object)
 */
@Getter
@AllArgsConstructor
public enum SmsCaptchaMonitorEnum implements CaptchaMonitor, IEnum<Integer>, EnumValueResolver<SmsCaptchaMonitorEnum, Integer> {

    /**
     * 邮箱验证码监控
     */
    DEFAULT(1, "不记录成功或者失败的日志") {
        /**
         * 监控成功/失败
         *
         * @param monitorRequest 监控请求
         */
        @Override
        public void monitor(MonitorRequest monitorRequest) {
            // TODO document why this method is empty
            super.monitor(monitorRequest);
        }

        /**
         * 计费
         *
         * @param monitorRequest 监控请求
         */
        @Override
        public void cost(MonitorRequest monitorRequest) {
            super.cost(monitorRequest);
        }
    },

    /**
     * 邮箱验证码监控
     */
    MONITOR(2, "采用spring的micrometer监控放到prometheus中") {
        /**
         * 监控成功/失败
         *
         * @param monitorRequest 监控请求
         */
        @Override
        public void monitor(MonitorRequest monitorRequest) {
//            SpringContext.getBean(MeterRegistry.class).counter(UserConstants.METER_SMS_SEND_COUNT,
//                    "tenantId",monitorRequest.getTenantId() ,
//                    "sendType", monitorRequest.getSendType(),
//                    "mailHost", monitorRequest.getValue(),
//                    "languageCode", monitorRequest.LanguageCode(),
//                    "success", monitorRequest.getFlag(),
//                    "reason", monitorRequest.getReason()
//            ).increment();
        }

        /**
         * 计费
         *
         * @param monitorRequest 监控请求
         */
        @Override
        public void cost(MonitorRequest monitorRequest) {
            //            SpringContext.getBean(MeterRegistry.class).counter(UserConstants.METER_SMS_SEND_COST,
//                    "tenantId",monitorRequest.getTenantId() ,
//                    "sendType", monitorRequest.getSendType(),
//                    "mailHost", monitorRequest.getValue(),
//                    "languageCode", monitorRequest.LanguageCode(),
//                    "success", monitorRequest.getFlag(),
//                    "reason", monitorRequest.getReason()
//            ).increment(monitorRequest.getCost().doubleValue());
        }
    };

    private final Integer code;
    private final String desc;

    /**
     * 通过code获取限制枚举
     *
     * @return 限制枚举
     */
    public static SmsCaptchaMonitorEnum getByCode() {
        Integer type = SpringContext.getBean(MonitorProperties.class).getSms().getType();
        if (Objects.isNull(type)) {
            type = EmailCaptchaMonitorEnum.DEFAULT.getCode();
        }
        for (SmsCaptchaMonitorEnum value : SmsCaptchaMonitorEnum.values()) {
            if (value.getCode().equals(type)) {
                return value;
            }
        }
        throw new ServiceException(ResultCodeEnum.METHOD_NOT_ALLOWED);
    }
}
